home *** CD-ROM | disk | FTP | other *** search
/ Champak 43 / Vol 43.iso / games / phit.swf / scripts / __Packages / Vector2D.as < prev   
Encoding:
Text File  |  2007-07-13  |  4.7 KB  |  224 lines

  1. class Vector2D
  2. {
  3.    var _x = 0;
  4.    var _y = 0;
  5.    function Vector2D(x, y)
  6.    {
  7.       this._x = x;
  8.       this._y = y;
  9.    }
  10.    function SetToZero()
  11.    {
  12.       this._x = this._y = 0;
  13.    }
  14.    function SetToRandomNormal()
  15.    {
  16.       this.SetToAngleNormal(_root.random.GetRandom() * 3.141592653589793 * 2);
  17.    }
  18.    function Set(v)
  19.    {
  20.       this._x = v._x;
  21.       this._y = v._y;
  22.    }
  23.    function GetCopy()
  24.    {
  25.       return new Vector2D(this._x,this._y);
  26.    }
  27.    function GetStringForm()
  28.    {
  29.       return "( " + this._x + ", " + this._y + " )";
  30.    }
  31.    function IsEqual(v)
  32.    {
  33.       return v._x == this._x && v._y == this._y;
  34.    }
  35.    function IsZero(epsilon)
  36.    {
  37.       return this._x <= epsilon && this._y <= epsilon;
  38.    }
  39.    function Magnitude()
  40.    {
  41.       return Math.sqrt(this.MagnitudeSquared());
  42.    }
  43.    function MagnitudeSafe()
  44.    {
  45.       var _loc2_ = this.MagnitudeSquared();
  46.       if(_loc2_ > 0)
  47.       {
  48.          return Math.sqrt(_loc2_);
  49.       }
  50.       return 0;
  51.    }
  52.    function MagnitudeSquared()
  53.    {
  54.       return this._x * this._x + this._y * this._y;
  55.    }
  56.    function GetDistance(v)
  57.    {
  58.       return this.GetSubtract(v).MagnitudeSafe();
  59.    }
  60.    function GetDistanceSquared(v)
  61.    {
  62.       return this.GetSubtract(v).MagnitudeSquared();
  63.    }
  64.    function Rotate(angleRadians)
  65.    {
  66.       var _loc3_ = Math.cos(angleRadians);
  67.       var _loc2_ = Math.sin(angleRadians);
  68.       var _loc5_ = _loc3_ * this._x - _loc2_ * this._y;
  69.       var _loc4_ = _loc2_ * this._x + _loc3_ * this._y;
  70.       this._x = _loc5_;
  71.       this._y = _loc4_;
  72.    }
  73.    function SetToAngleNormal(angleRadians)
  74.    {
  75.       this._x = Math.cos(angleRadians);
  76.       this._y = Math.sin(angleRadians);
  77.    }
  78.    function Normalize()
  79.    {
  80.       var _loc2_ = this.Magnitude();
  81.       if(_loc2_ > 0)
  82.       {
  83.          this._x /= _loc2_;
  84.          this._y /= _loc2_;
  85.       }
  86.    }
  87.    function GetNormal()
  88.    {
  89.       var _loc2_ = new Vector2D(this._x,this._y);
  90.       _loc2_.Normalize();
  91.       return _loc2_;
  92.    }
  93.    function Invert()
  94.    {
  95.       this._x = - this._x;
  96.       this._y = - this._y;
  97.    }
  98.    function Transpose()
  99.    {
  100.       var _loc2_ = this._x;
  101.       this._x = this._y;
  102.       this._y = _loc2_;
  103.    }
  104.    function GetInverse()
  105.    {
  106.       return new Vector2D(- this._x,- this._y);
  107.    }
  108.    function GetAngle()
  109.    {
  110.       return Math.atan2(this._y,this._x);
  111.    }
  112.    function GetAngleDegrees()
  113.    {
  114.       return Math.atan2(this._y,this._x) / 3.141592653589793 * 180;
  115.    }
  116.    function AddScalar(scalar)
  117.    {
  118.       this._x += scalar;
  119.       this._y += scalar;
  120.    }
  121.    function SubtractScalar(scalar)
  122.    {
  123.       this._x -= scalar;
  124.       this._y -= scalar;
  125.    }
  126.    function MultiplyScalar(scalar)
  127.    {
  128.       this._x *= scalar;
  129.       this._y *= scalar;
  130.    }
  131.    function DivideScalar(scalar)
  132.    {
  133.       this._x /= scalar;
  134.       this._y /= scalar;
  135.    }
  136.    function Add(v)
  137.    {
  138.       this._x += v._x;
  139.       this._y += v._y;
  140.    }
  141.    function Subtract(v)
  142.    {
  143.       this._x -= v._x;
  144.       this._y -= v._y;
  145.    }
  146.    function Multiply(v)
  147.    {
  148.       this._x *= v._x;
  149.       this._y *= v._y;
  150.    }
  151.    function Divide(v)
  152.    {
  153.       this._x /= v._x;
  154.       this._y /= v._y;
  155.    }
  156.    function DotProduct(v)
  157.    {
  158.       return this._x * v._x + this._y * v._y;
  159.    }
  160.    function GetAddScalar(scalar)
  161.    {
  162.       var _loc2_ = new Vector2D(this._x,this._y);
  163.       _loc2_.AddScalar(scalar);
  164.       return _loc2_;
  165.    }
  166.    function GetSubtractScalar(scalar)
  167.    {
  168.       var _loc2_ = new Vector2D(this._x,this._y);
  169.       _loc2_.SubtractScalar(scalar);
  170.       return _loc2_;
  171.    }
  172.    function GetMultiplyScalar(scalar)
  173.    {
  174.       var _loc2_ = new Vector2D(this._x,this._y);
  175.       _loc2_.MultiplyScalar(scalar);
  176.       return _loc2_;
  177.    }
  178.    function GetDivideScalar(scalar)
  179.    {
  180.       var _loc2_ = new Vector2D(this._x,this._y);
  181.       _loc2_.DivideScalar(scalar);
  182.       return _loc2_;
  183.    }
  184.    function GetAdd(v)
  185.    {
  186.       var _loc2_ = new Vector2D(this._x + v._x,this._y + v._y);
  187.       return _loc2_;
  188.    }
  189.    function GetSubtract(v)
  190.    {
  191.       var _loc2_ = new Vector2D(this._x - v._x,this._y - v._y);
  192.       return _loc2_;
  193.    }
  194.    function GetMultiply(v)
  195.    {
  196.       var _loc2_ = new Vector2D(this._x * v._x,this._y * v._y);
  197.       return _loc2_;
  198.    }
  199.    function SnapToMajorAxis()
  200.    {
  201.       if(Math.abs(this._x) > Math.abs(this._y))
  202.       {
  203.          this._y = 0;
  204.       }
  205.       else
  206.       {
  207.          this._x = 0;
  208.       }
  209.       this.Normalize();
  210.    }
  211.    function SnapToMinorAxis()
  212.    {
  213.       if(Math.abs(this._x) < Math.abs(this._y))
  214.       {
  215.          this._y = 0;
  216.       }
  217.       else
  218.       {
  219.          this._x = 0;
  220.       }
  221.       this.Normalize();
  222.    }
  223. }
  224.